/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.RecipeHandler;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.ScrollPaneConstants;

/**
 *
 * @author Jonas
 */
public class SearchPanel extends javax.swing.JPanel {

    private ArrayList<RecipePanel> rpList;
    private RecipeHandler rh;

    /**
     * Creates new form SearchPanel
     */
    public SearchPanel(RecipeHandler rh) {
        this.rh = rh;
        rpList = new ArrayList();
        initComponents();
        jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        setSize(600, 300);
    }

    public JPanel getResultPane() {
        return jResultPane;
    }

    public ArrayList<RecipePanel> getRpList() {
        return rpList;
    }

    public void addRecipe(RecipePanel rp) {
        rpList.add(rp);
    }

    public void clearRpList() {
        rpList.clear();
    }

    public void showRpList(ArrayList<RecipePanel> rpShowList) {
        if (!rpShowList.isEmpty()) {
            int y = 0;
            for (RecipePanel rp : rpShowList) {
                getResultPane().add(rp);
                rp.setLocation(0, y);
                y = y + 100;
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField1 = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jResultPane = new javax.swing.JPanel();

        jTextField1.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        jTextField1.setText("Søg opskrift..");
        jTextField1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(255, 255, 255), 1, true));
        jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextField1FocusGained(evt);
            }
        });
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField1KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        jResultPane.setBackground(new java.awt.Color(197, 0, 7));
        jResultPane.setPreferredSize(new java.awt.Dimension(600, 300));

        javax.swing.GroupLayout jResultPaneLayout = new javax.swing.GroupLayout(jResultPane);
        jResultPane.setLayout(jResultPaneLayout);
        jResultPaneLayout.setHorizontalGroup(
            jResultPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 600, Short.MAX_VALUE)
        );
        jResultPaneLayout.setVerticalGroup(
            jResultPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jResultPane);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed

    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped

    }//GEN-LAST:event_jTextField1KeyTyped

    private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyReleased
        showResult();

    }//GEN-LAST:event_jTextField1KeyReleased

    private void jTextField1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextField1FocusGained
        jTextField1.selectAll();
    }//GEN-LAST:event_jTextField1FocusGained

    public void showResult() {
        if (!jTextField1.getText().isEmpty()) {
            getResultPane().removeAll();
            showRpList(rh.findRecipe(jTextField1.getText()));
        } else {
            getResultPane().removeAll();
        }
        repaint();
        revalidate();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jResultPane;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
